package com.example.service.impl;

import com.example.entity.detection.DetectionElevator;
import com.example.entity.detection.ExtDetectionElevator;
import com.example.mapp.DetectionElevatorMapp;
import com.example.service.DetectionElevatorService;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DetectionElevatorServiceImpl implements DetectionElevatorService {

    private static final Logger logger = LoggerFactory.getLogger(DetectionElevatorServiceImpl.class);

    @Autowired
    private DetectionElevatorMapp detectionElevatorMapp;

    @Override
    public Map<String, Object> query(ExtDetectionElevator detectionElevator) {
        Map<String,Object> resultMap = new HashMap<>();
        try{
            if((detectionElevator.getCurrentPage()+"") != null && (detectionElevator.getPageSize()+"") != null){
                Page<ExtDetectionElevator> page = PageHelper.startPage(detectionElevator.getCurrentPage(),detectionElevator.getPageSize());
                List<ExtDetectionElevator> list = detectionElevatorMapp.query(detectionElevator);
                resultMap.put("list",list);
                resultMap.put("total",page.getTotal());
            }
            return ResultUtil.returnSuccess("查询成功", resultMap);
        }catch (Exception e){
            logger.error("检测电梯查询失败！",e);
            return ResultUtil.returnError("查询失败",null);
        }
    }

    @Override
    public Map<String, Object> add(List<DetectionElevator> detectionElevatorList) {
        try{
            if(CollectionUtils.isEmpty(detectionElevatorList)){
                return ResultUtil.returnError("新增失败，传入的电梯数据为空",null);
            }
            for(DetectionElevator detectionElevator : detectionElevatorList){
                detectionElevator.setIsPass("0"); //未检查状态
                detectionElevatorMapp.insert(detectionElevator);
            }

            return ResultUtil.returnSuccess("新增成功", null);
        }catch (Exception e){
            logger.error("检测电梯添加失败！",e);
            return ResultUtil.returnError("新增失败",null);
        }
    }

    /**
     * 电梯检测完成，生成原始记录与检测报告
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> update(Map map) {
        try{
            detectionElevatorMapp.update(map);
            return ResultUtil.returnSuccess("更新成功", null);
        }catch (Exception e){
            logger.error("检测电梯更新失败！",e);
            return ResultUtil.returnError("更新失败",null);
        }
    }


    @Override
    public Map<String, Object> countElevator(Map map) {
        try{
            Map<String,Object> resultMap =  detectionElevatorMapp.countElevator(map);
            return ResultUtil.returnSuccess("查询成功", resultMap);
        }catch (Exception e){
            logger.error("检测电梯查询失败！",e);
            return ResultUtil.returnError("查询失败",null);
        }
    }

}
